package titt.util.search;

import titt.util.Bytes;

public class ByteSequence {

	private final int[] seq;
	private final byte[] prefix, suffix;
	private final byte seq_byte;
		
	public ByteSequence(byte[] b) {
		seq = findLargestSequence(b);
		int seq_end = getSequenceIndex() + getSequenceLength();
		
		seq_byte = b[getSequenceIndex()];
		prefix = Bytes.copy(b, 0, getSequenceIndex());
		suffix = Bytes.copy(b, seq_end, b.length - seq_end);
	}
	
	public int getSequenceLength() {
		return seq[1];
	}
	
	public int getSequenceIndex() {
		return seq[0];
	}

	public byte getSequenceByte() {
		return seq_byte;
	}
	
	public byte[] getPrefix() {
		return prefix;
	}
	
	public byte[] getSuffix() {
		return suffix;
	}
	
	private int[] findLargestSequence(byte[] b) {
		int[] ret = new int[] { 0, 1 }, cur = ret;
		
		for(int i = 1; i < b.length; i++) {
			if(b[i] == b[cur[0]]) cur[1]++;
			else {
				if(cur[1] > ret[1]) ret = cur;
				cur = new int[]{ i, 1 };
			}
		}
		if(cur[1] > ret[1]) ret = cur;
		
		return ret;
	}
}
